using System;
using System.Collections.Generic;
using System.Text;
using Utils;
using System.IO;
using Utils.WebFramework;
using System.Web;
using System.Collections;
namespace Utils.WebFramework
{
    public class ActionResult : NameValues
    {
        public ActionResult()
        {

        }

        public Encoding Encoding
        {
            get { return encoding;  }
            set { encoding = value; }
        }

        protected Encoding encoding = Framework.GetFramework().Encoding;
    }

    public class ViewResult : ActionResult
    {
        public ViewResult()
        {

        }

        public ViewResult(string view)
        {
            this.view = view;
        }

        internal string ExecViewPath(string controller)
        {
            return Framework.GetFramework().ViewsPath + "/" + controller + "/" + view + ".aspx";
        }

        internal string view = string.Empty;
    }

    public class HttpResult : ActionResult
    {
        public HttpResult()
        {

        }

        public string ContentType
        {
            get { return contentType;  }
            set { contentType = value; }
        }

        public void AddHeader(string name, string value)
        {
            headers[name] = value;
        }

        public void Write(string s)
        {
            byte[] bs = encoding.GetBytes(s);
            stream.Write(bs, 0, bs.Length);
        }

        public void Write(byte[] bs)
        {
            stream.Write(bs, 0, bs.Length);
        }

        public Stream Stream
        {
            get { return stream; }
        }

        internal string contentType  = "text/html";
        internal Hashtable headers   = new Hashtable();
        internal MemoryStream stream = new MemoryStream();
    }

    public class ReDirectResult : ActionResult
    {
        public ReDirectResult()
        {

        }

        public ReDirectResult(string controller, string action, Httper referrer)
        {
            this.controller = controller;
            this.action     = action;
            this.referrer   = referrer;
        }

        public string Controller
        {
            get { return controller; }
            set { controller = value; }
        }

        public string Action
        {
            get { return action;  }
            set { action = value; }
        }

        internal Httper Httper 
        {
            get 
            {
                Httper httper = new Httper(controller, action, referrer);
                httper.AddNameValues(this);
                return httper;
            }
        }

        private string controller = string.Empty;
        private string action     = string.Empty;
        private Httper referrer   = null;
    }

    public class ReDirectUrlResult : ActionResult
    {
        public ReDirectUrlResult(string url)
        {
            this.url = url;
        }

        internal string Url 
        {
            get 
            {
                string ret = url;

                if ( ret.IndexOf('?') != -1 )
                {
                    ret += "&";
                }

                string query = string.Empty;
                foreach (string name in this.nameValues.Keys)
                {
                    query += Html.UrlEncode(name, encoding) + "=" + Html.UrlEncode(name ^ this,Encoding.UTF8) + "&";
                }

                if ( query != string.Empty )
                {
                    ret += query;
                }

                return ret;
            }
        }

        internal string url = string.Empty;
    }
}
